<?php
/**
 * Each engineer has a duty to keep the code elegant
 * Created by wolffy.
 * Author @wolffy<13750088090@sina.cn>
 * Date: 2018/8/12
 * Time: 11:40
 */
namespace App\Repositories;

use App\Models\DistributGroup;

class DistributGroupRepository
{
    public function create(array $params)
    {
        return DistributGroup::create($params);
    }

    public function insertGetId(array $params)
    {
        return DistributGroup::insertGetId($params);
    }

    public function isBindGroupMember($groupMobile)
    {
        return DistributGroup::where([
            'group_mobile' => $groupMobile,
            'is_bind' => 1,
        ])->first();
    }

    public function isExistGroupMember($distributId, $groupMobile)
    {
        return DistributGroup::where([
            'distribut_id' => $distributId,
            'group_mobile' => $groupMobile,
        ])->first();
    }

    /**
     * @param $distributId
     * @param $groupUserId
     * @return mixed
     */
    public function findGroupMember($distributId, $groupUserId)
    {
        return DistributGroup::where([
            'distribut_id' => $distributId,
            'group_user_id' => $groupUserId,
            'is_bind' => 1,
        ])->first();
    }

    public function groupMemberCountLimit($distributId)
    {
        $count = DistributGroup::where([
            'distribut_id' => $distributId,
        ])
            ->whereIn('status', [0, 1, 2])
            ->count();
        return (string)$count;
    }

    /**
     * @param $distributId
     * @return mixed
     */
    public function groupMemberCount($distributId)
    {
        $count = DistributGroup::where([
            'distribut_id' => $distributId,
            'is_bind' => 1,
        ])->count();
        return (string)$count;
    }

    /**
     * @param $distributId
     * @return mixed
     */
    public function groupMemberCountWaitPass($distributId)
    {
        $count = DistributGroup::where([
            'distribut_id' => $distributId,
        ])
            ->whereIn('status', [0, 1])
            ->count();
        return (string)$count;
    }

    public function byId($groupId)
    {
        return DistributGroup::find($groupId);
    }

    public function getGroupMember($distributId)
    {
        return DistributGroup::where([
            'distribut_id' => $distributId,
            'is_bind' => 1,
        ])->get();
    }

    public function getGroupMemberIds($distributId)
    {
        return DistributGroup::where([
            'distribut_id' => $distributId,
            'is_bind' => 1,
        ])
            ->select('id', 'group_user_id')
            ->get();
    }
}